module.exports = app => {
  const mongoose = app.mongoose,
        Schema = mongoose.Schema,
        ObjectId = Schema.Types.ObjectId;
  /**
   * @ reply data schema
   */
  let ReplySchema = new Schema({
    toComment: { type: ObjectId, ref: 'Comment' },  // 评论ID
    toAuthor: { type: ObjectId, ref: 'Member' },  // 评论人ID
    author: { type: ObjectId, ref: 'Member' },  // 当前回复者ID
    content: { type: String, default: '' },
    date: { type: Date, default: Date.now() }
  }, {usePushEach: true});
  ReplySchema.statics = {
    add(data) {
      console.log('17.....', data);
      return data.save();
    },
    // update(params, cb) {
    //   return this.findByIdAndUpdate({_id: params.toComment}, {$push: {reply: params.rid}}, {new: true}, cb);
    // },
    // findAll(aid, cb) {
    //   return this.find({article: aid})
    //     .populate('author', {'_id':1,'username':1,'avatarsPath':1})
    //     // .populate('toAuthor', {'_id': 1,'username':1,'avatarsPath':1})
    //     // .populate({path:'reply',populate:[{path:'author',select:{'_id':1,'username':1,'avatarsPath':1}},{path:'toAuthor',select:{'_id':1,'username':1,'avatarsPath':1}}]})
    //     .exec(cb);
    // },
    findAll(cid, cb) {
      return this.find({toComment: cid})
        .populate('author', {'_id':1,'username':1,'avatarsPath':1})
        .populate('toAuthor', {'_id': 1,'username':1,'avatarsPath':1})
        // .populate({
        //   path:'reply',
        //   populate:[{
        //     path:'author',
        //     select:{'_id':1,'username':1,'avatarsPath':1}
        //   }, {
        //     path:'toAuthor',
        //     select:{'_id':1,'username':1,'avatarsPath':1}
        //   }]
        // })
        .exec(cb);
    }
  };
  
  return mongoose.model('Reply', ReplySchema);
}





// const mongoose = require('mongoose');
// const CommentSchema = require('../schemas/comment');
// const Comment = mongoose.model('Comment', CommentSchema);

// module.exports = Comment;